﻿(function () {

    angular.module('app').controller("mainController", function ($rootScope, $scope, $state) {

        $scope.tabs = [
            /*{ heading: "Tab 1", route: "main.tab1", active: false },
            { heading: "Tab 2", route: "main.tab2", active: false },
            { heading: "Tab 3", route: "main.tab3", active: false },*/
        ];

        $scope.go = function (route) {
            $state.go(route);
        };

        $scope.active = function (route) {
            return $state.is(route);
        };

        $scope.close = function (route) {
            _.remove($scope.tabs, function (item) {
                return item.route === route;
            });
            $scope.active('main.tenants');
        }

        $scope.$on("$stateChangeSuccess", function (event, toState, toParams, fromState, fromParams) {

            abp.log.debug({
                'event': event,
                'toState': toState,
                'toParams': toParams,
                'fromState': fromState,
                'fromParams': fromParams
            });

            var isexist=_.find($scope.tabs, 
                function(item) {
                     return item.route===toState.name;
                });

            if (isexist == null) {
                $scope.tabs.push({
                    heading: toState.menu,
                    route: toState.name,
                    active: false,
                    close: toState.name !== 'main.tenants'
                });
            }

            $scope.tabs.forEach(function (tab) {
                tab.active = $scope.active(tab.route);
            });
        });
    });
})();